package vip.zhenzicheng.nettyadv.cryocodec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import vip.zhenzicheng.nettyadv.util.EncryptUtils;
import vip.zhenzicheng.nettyadv.vo.MakeMsgID;
import vip.zhenzicheng.nettyadv.vo.MsgHeader;
import vip.zhenzicheng.nettyadv.vo.MyMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * 序列化器的测试类
 *
 * @author zhenzicheng
 * @date 2022-06-14 10:30
 */
public class TestKryoCodec {

  /**
   *
   */
  public static void main(String[] args) throws Exception {
    TestKryoCodec testC = new TestKryoCodec();

    for (int i = 0; i < 5; i++) {
      ByteBuf sendBuf = Unpooled.buffer();
      MyMessage message = testC.getMessage(i);
      System.out.println("Encode:" + message);
      KryoSerializer.serialize(message, sendBuf);
      MyMessage decodeMsg = (MyMessage) KryoSerializer.deserialize(sendBuf);
      System.out.println("Decode:" + decodeMsg);
      System.out
          .println("-------------------------------------------------");
    }

  }

  public MyMessage getMessage(int j) {
    String content = "abcdefg--------AAAAAA:" + j;
    MyMessage myMessage = new MyMessage();
    MsgHeader msgHeader = new MsgHeader();
    msgHeader.setMsgID(MakeMsgID.getID());
    msgHeader.setType((byte) 1);
    msgHeader.setPriority((byte) 7);
    msgHeader.setMd5(EncryptUtils.encryptObj(content));
    Map<String, Object> attachment = new HashMap<>();
    for (int i = 0; i < 10; i++) {
      attachment.put("city --> " + i, "mark " + i);
    }
    msgHeader.setAttachment(attachment);
    myMessage.setMsgHeader(msgHeader);
    myMessage.setBody(content);
    return myMessage;
  }
}
